// store/modules/order.js
import axios from 'axios';

export default {
    namespaced: true,
    state: {
        orderDetails: null,
        error: null,
    },
    mutations: {
        SET_ORDER_DETAILS(state, orderDetails) {
            state.orderDetails = orderDetails;
        },
        SET_ERROR(state, error) {
            state.error = error;
        },
    },
    actions: {
        async loadOrderDetails({ commit }, orderId) {
            try {
                const response = await axios.get(`order/api/order/detail/travel/${orderId}`);
                if (response.data.code === 200) {
                    commit('SET_ORDER_DETAILS', response.data.data);
                    return { success: true };
                } else {
                    commit('SET_ERROR', response.data.message || '加载订单详情失败');
                    return { success: false, error: response.data.message || '加载订单详情失败' };
                }
            } catch (error) {
                console.error('加载订单详情失败:', error);
                const errorMessage = '加载订单详情失败，请稍后再试';
                commit('SET_ERROR', errorMessage);
                return { success: false, error: errorMessage };
            }
        },
    },
    getters: {
        getOrderDetails: state => state.orderDetails,
        getError: state => state.error,
    },
};
